package com.hfut.jpoa.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hfut.jpoa.common.spring.SpringHelper;
import com.hfut.jpoa.service.config.SystemConfigUtil;

public class ContextListener implements ServletContextListener {
    
    private ServletContext context = null;
    
    private WebApplicationContext springContent = null;
    
    public void contextDestroyed(ServletContextEvent arg0) {
        /** clear timer **/
        
    }
    
    public void contextInitialized(ServletContextEvent event) {
        
        this.context = event.getServletContext();
        
        springContent = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        
        //初始化SpringHelper环境变量
        SpringHelper.getSpringHelper().initApplicationContext(springContent);
        
        //设置资源路径
        String resourcePath = SystemConfigUtil.getPropertyFromDC("RESOURCE_PATH");
        
        //       resourcePath = "http://localhost";
        context.setAttribute("resourcePath", resourcePath);
        
    }
    
    public WebApplicationContext getSpringContent() {
        return springContent;
    }
    
}
